﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

public partial class UserAreaRights : ExtBaseRpt
{
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            this.GridPanel3.GetStore().DataSource = mydb.DM_GetDateSet("select * from Sys_LoginUser where Block = 1");
            

            RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
            sm.SelectedRows.Add(new SelectedRow(0));
            //sm.SelectedRows.Add(new SelectedRow("Caterpillar Inc."));
            InsActLog(mid, "浏览");
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        
    }
    protected void Btn_Add(object sender, DirectEventArgs e)
    {
        RowSelectionModel user = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
        string item="";
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        foreach (SelectedRow row in sm.SelectedRows)
        {
            item += "'"+row.RecordID+"',";
        }

        string sql = @"insert into Sys_UserAreaRights(userid,stype,sourceid,sourcename,creator)
        select '"+user.SelectedRow.RecordID+"',0,m.*,'"+Session["UserID"]+"' from (select distinct OperationAreaID,OperationAreaName from V_ShopList)m where OperationAreaID in(" + item.TrimEnd(',') + ")";
        //ShowBox(sql);
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            OpenRights(user.SelectedRow.RecordID);
        }
    }
    protected void Btn_Delete(object sender, DirectEventArgs e)
    {
        RowSelectionModel user = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
        string item = "";
        RowSelectionModel sm = this.GridPanel2.GetSelectionModel() as RowSelectionModel;
        foreach (SelectedRow row in sm.SelectedRows)
        {
            item += "'" + row.RecordID + "',";
        }

        string sql = @"Delete from Sys_UserAreaRights where userid='" + user.SelectedRow.RecordID + "' and  sourceid in(" + item.TrimEnd(',') + ")";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            OpenRights(user.SelectedRow.RecordID);
        }
    }
    protected void Cell_Click(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel3.GetSelectionModel() as RowSelectionModel;
        //ShowBox(sm.SelectedRow.RecordID);
        OpenRights(sm.SelectedRow.RecordID);
    }
    protected void OpenRights(string userid)
    {
        this.GridPanel1.GetStore().DataSource = mydb.DM_GetDateSet(@"select distinct OperationAreaID,OperationAreaName from V_ShopList 
        where OperationAreaID not in ( select SourceID from Sys_UserAreaRights where userid='"+userid+"') order by OperationAreaID");
        this.GridPanel1.GetStore().DataBind();

        this.GridPanel2.GetStore().DataSource = mydb.DM_GetDateSet(@"select * from Sys_UserAreaRights where  userid='" + userid + "'");
        this.GridPanel2.GetStore().DataBind();
    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
}